package com.ebt.m.commons.buscomponent.thirtylogin.model;

import android.app.Dialog;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

public abstract class ShareListener implements IUiListener, IWXAPIEventHandler {
	
	protected String mPlatform;

	public void setPlatform(String platform){
		this.mPlatform = platform;
	}
	
	public String getPlatform(){
		return this.mPlatform;
	}

    public void onShareCompleteWrapper(){
        onShareComplete(mPlatform);
        if (dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }

    public void onShareErrorWrapper(String errorInfo){
        onShareError(mPlatform, errorInfo);
    }

    public void onShareCancelWrapper(){
        onShareCancel(mPlatform);
    }

	public abstract void onShareComplete(String platform);

	public abstract void onShareError(String platform, String errorInfo);

    public abstract void onShareCancel(String platform);

    public Dialog dialog;

    public void setDialog(Dialog dialog){
        this.dialog = dialog;
    }

    //QQ or QZone
    @Override
    public void onComplete(Object o) {
        onShareComplete(mPlatform);
        if (dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }

    //QQ or QZone
    @Override
    public void onError(UiError uiError) {
        onShareError(mPlatform, uiError == null ? "null" : uiError.errorMessage);
    }

    //QQ or QZone
    @Override
    public void onCancel() {
        onShareCancel(mPlatform);
    }

    // WeChat
    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            default:
                break;
        }
    }

    // WeChat
    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                onShareComplete(mPlatform);
                if (dialog != null && dialog.isShowing()){
                    dialog.dismiss();
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                onShareCancel(mPlatform);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                onShareError(mPlatform, resp == null ? "null" : resp.errStr);
                break;
            default:
                onShareError(mPlatform, resp == null ? "null" : resp.errStr);
                break;
        }
    }
}